package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.paas.field.SerialNumberPoJo;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.dictionary.ArchivedEnum;
import com.xbongbong.saas.enums.dictionary.OpportunityWastageEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/1/17 19:35
 * @since v1.0
 */
@Getter
public enum TempSalesOpportunityEnum {
    /**
     * 销售机会枚举类
     */

    BASE_INFO(SalesOpportunityEnum.BASE_INFO),
    NAME(SalesOpportunityEnum.NAME),
    SERIAL_NO(SalesOpportunityEnum.SERIAL_NO),
    CUSTOMER_NAME(SalesOpportunityEnum.CUSTOMER_NAME),
    CONTACT_NAME(SalesOpportunityEnum.CONTACT_NAME),
    SALE_STAGE(SalesOpportunityEnum.SALE_STAGE),
    WASTAGE(SalesOpportunityEnum.WASTAGE),
    WASTAGE_MEMO(SalesOpportunityEnum.WASTAGE_MEMO),
    OWNER_ID(SalesOpportunityEnum.OWNER_ID),
    COUSER_ID(SalesOpportunityEnum.COUSER_ID),

    SALE_INFO(SalesOpportunityEnum.SALE_INFO),
    PRODUCTS(SalesOpportunityEnum.PRODUCTS),
    ESTIMATE_AMOUNT(SalesOpportunityEnum.ESTIMATE_AMOUNT),
    ESTIMATE_END_TIME(SalesOpportunityEnum.ESTIMATE_END_TIME),
    COST(SalesOpportunityEnum.COST),
    GROSS_PROFIT(SalesOpportunityEnum.GROSS_PROFIT),
    GROSS_PROFIT_RATE(SalesOpportunityEnum.GROSS_PROFIT_RATE),

    EXTEND_INFO(SalesOpportunityEnum.EXTEND_INFO),
    IMPORTANT_DEGREE(SalesOpportunityEnum.IMPORTANT_DEGREE),
    DECISION_MAKER(SalesOpportunityEnum.DECISION_MAKER),
    COMPETITOR(SalesOpportunityEnum.COMPETITOR),


    SYSTEM_INFO(SalesOpportunityEnum.SYSTEM_INFO),
    CREATOR_ID(SalesOpportunityEnum.CREATOR_ID),
    ADD_TIME(SalesOpportunityEnum.ADD_TIME),
    UPDATE_TIME(SalesOpportunityEnum.UPDATE_TIME),

    LAST_CONNECT_TIME(SalesOpportunityEnum.LAST_CONNECT_TIME),
    IS_ARCHIVED(SalesOpportunityEnum.IS_ARCHIVED),

    UNIT(SalesOpportunityEnum.UNIT),
    EXCHANGE_RATE(SalesOpportunityEnum.EXCHANGE_RATE),
    CUSTOMER_NAME_LINK_TEXT(SalesOpportunityEnum.CUSTOMER_NAME_LINK_TEXT),
    CONTACT_NAME_LINK_TEXT(SalesOpportunityEnum.CONTACT_NAME_LINK_TEXT),
    DECISION_MAKER_ID_LINK_TEXT(SalesOpportunityEnum.DECISION_MAKER_ID_LINK_TEXT),

    OTHER_CHARGE(SalesOpportunityEnum.OTHER_CHARGE),

    STAGE_PROCESS_ID(SalesOpportunityEnum.STAGE_PROCESS_ID),
    STAGE_ID(SalesOpportunityEnum.STAGE_ID),
    STAGE_RATIO(SalesOpportunityEnum.STAGE_RATIO),

    LINK_COMPETITOR(SalesOpportunityEnum.LINK_COMPETITOR),
    WIN_COMPETITOR(SalesOpportunityEnum.WIN_COMPETITOR),
    LINK_COMPETITOR_LINK_TEXT(SalesOpportunityEnum.LINK_COMPETITOR_LINK_TEXT),
    WIN_COMPETITOR_LINK_TEXT(SalesOpportunityEnum.WIN_COMPETITOR_LINK_TEXT),

    ;
    private SalesOpportunityEnum anEnum;

    TempSalesOpportunityEnum(SalesOpportunityEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempSalesOpportunityEnum> getAllEnum() {
        List<TempSalesOpportunityEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }


    public static void initStageProcessId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsRedundant(0);
    }

    public static void initStageId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsRedundant(0);
    }

    public static void initStageRatio(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsRedundant(0);
        fieldAttrEntity.setIsOpen(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setAccuracy(0);
    }

    /**
     * 初始化机会名称
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initName(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(NAME.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(0);
    }

    /**
     * 初始化机会编号
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initOpportunityNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(SERIAL_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        // 默认的编号规则
        SerialNumberPoJo serialNumberPoJo = InitExplainsHelper.setSerialNumber(SerialNumberEnum.SALE_OPPORTUNITY_NO);
        fieldAttrEntity.setSerialNumber(serialNumberPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化币种
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initUnit(FieldAttrEntity fieldAttrEntity) {
        String str = "元,美元,欧元,日元,万,港币,万元";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItem(str));
    }

    /**
     * 初始化归档状态
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initIsArchived(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ArchivedEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initWastage(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(OpportunityWastageEnum.class, fieldAttrEntity.getFieldType()));
    }

    /**
     * 初始化关联客户
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initCustomerName(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CUSTOMER.getCode());
        fieldAttrEntity.setLinkedText(CUSTOMER_NAME_LINK_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化关联联系人
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initContactName(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CONTACT.getCode());
        fieldAttrEntity.setLinkedText(CONTACT_NAME_LINK_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("relied");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 初始化销售阶段
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initSaleStage(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化关联决策人
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initDecisionMaker(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CONTACT.getCode());
        fieldAttrEntity.setLinkedText(DECISION_MAKER_ID_LINK_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("relied");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 初始化预计结束时间
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initEstimateEndTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initEstimateAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(1);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + SalesOpportunityEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.售价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
    }

    public static void initCost(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(1);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("机会成本为产品成本");
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initGrossProfit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(1);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("机会毛利 = 预计金额 - 产品成本");
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initGrossProfitRate(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.AVERAGE.getType());
        fieldAttrEntity.setMemo("机会毛利率 = 机会毛利/预计金额*100%");
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initLastConnectTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRecentTime(true);
        fieldAttrEntity.setDateType(DateTimeEnum.DATETIME.getType());
    }

    public static void initCompetitor(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setMemo("请输入竞争对手(多个用逗号隔开)");
    }

    public static void initCoUserId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setRequired(0);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增协同人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setSetType(3);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }
    public static void initOwnerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setRequired(1);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增负责人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        //可删除权限：默认【超级管理员、销售主管】可删除负责人，支持自定义；
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setMultiple(1);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }
    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    /**
     * 初始化关联竞争对手
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initLinkCompetitor(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.COMPETITOR.getCode());
        fieldAttrEntity.setLinkedText(LINK_COMPETITOR_LINK_TEXT.anEnum.getAttr());
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setAdd(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setShowAdd(0);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.COMPETITOR.getCode());
        fieldAttrEntity.setShowEditable(0);
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyMode("competitor");
        saasParticularAttributePoJo.setRelyType("base");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.COMPETITOR.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setMemo("此字段限旗舰版套餐使用，维护商机中竞争对手情况");
        fieldAttrEntity.setIsOpen(0);
    }
    /**
     * 初始化赢单竞品
     * date: 2022/8/15 11:22 上午
     * @author yingjf
     * @param fieldAttrEntity:  字段解释实体--saas专用
     */
    public static void initWinCompetitor(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.COMPETITOR.getCode());
        fieldAttrEntity.setLinkedText(WIN_COMPETITOR_LINK_TEXT.anEnum.getAttr());
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.COMPETITOR.getCode());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("competitor");
        saasParticularAttributePoJo.setRelyType("base");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.COMPETITOR.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setMemo("此字段限旗舰版套餐使用，维护商机中竞争对手情况");
        fieldAttrEntity.setIsOpen(0);
    }
}
